package com.example.service.message.controller;


import com.example.common.ResponseVO;
import com.example.common.enums.command.GroupEventCommand;
import com.example.common.model.SyncReq;
import com.example.common.model.message.CheckSendMessageReq;
import com.example.service.group.service.GroupMessageService;
import com.example.service.message.model.req.SendMessageReq;
import com.example.service.message.service.MessageSyncService;
import com.example.service.message.service.P2PMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("v1/message")
@Api(tags = "消息发送的接口")
public class MessageController {

    @Autowired
    P2PMessageService p2PMessageService;

    @Autowired
    GroupMessageService groupMessageService;

    @Autowired
    MessageSyncService messageSyncService;

    @PostMapping("/send")
    @ApiOperation("单聊消息的发送")
    public ResponseVO send(@RequestBody @Validated SendMessageReq req)  {
//        req.setAppId(appId);
        return ResponseVO.successResponse(p2PMessageService.send(req));
    }


    @PostMapping("/checkSend")
    @ApiOperation("发送消息进行的校验（这边会被fegin调用）")
    public ResponseVO checkSend(@RequestBody @Validated CheckSendMessageReq req)  {
        if(req.getCommand().equals(GroupEventCommand.MSG_GROUP.getCommand())){
            return groupMessageService.imServerPermissionCheck(req.getFromId(),req.getToId(),req.getAppId());
        }
        ResponseVO responseVO = p2PMessageService.imServerPermissionCheck(
                req.getFromId(), req.getToId(), req.getAppId()
        );
        return responseVO;
    }

    @PostMapping("/syncOfflineMessage")
    @ApiOperation("进行同步离线消息")
    public ResponseVO syncOfflineMessage(@RequestBody @Validated SyncReq req)  {
        return messageSyncService.syncOfflineMessage(req);
    }






}
